package com.rytx.web.controller.ysp;

import com.rytx.common.annotation.Log;
import com.rytx.common.core.controller.BaseController;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.core.domain.R;
import com.rytx.common.core.domain.model.LoginUser;
import com.rytx.common.core.page.TableDataInfo;
import com.rytx.common.enums.BusinessType;
import com.rytx.common.utils.poi.ExcelUtil;
import com.rytx.system.domain.ysp.*;
import com.rytx.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 艺术品Controller
 *
 * @author rytx
 * &#064;date  2024-05-13
 */
@RestController
@RequestMapping("/system/rental")
public class ArtRentalController extends BaseController
{
    @Resource
    private IArtRentalService artRentalService;
    @Resource
    private IPCategoryService iPCategoryService;
    @Resource
    private IPPlacementService iPPlacementService;
    @Resource
    private IPThemeService iPThemeService;
    @Resource
    private IPStyleService iPStyleService;
    @Resource
    private IArtViewHistoryService artViewHistoryService;
    @Resource
    private IArtFavoriteService artFavoriteService;


    /**
     * 查询艺术品列表
     */
    @PreAuthorize("@ss.hasPermi('system:rental:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArtRental artRental)
    {
        startPage();
        List<ArtRental> list = artRentalService.selectArtRentalList(artRental);
        return getDataTable(list);
    }
    /**
     * 商品浏览数增加
    */
    @PostMapping("/vh")
    public R<Integer> viewHistory(ArtViewHistory artViewHistory){
        return R.ok(artViewHistoryService.insertArtViewHistory(artViewHistory));
    }
    /**
     * 收藏
     */
    @PostMapping("/f")
    public R<Integer> insertFavorite(@Validated @RequestBody ArtFavorite artFavorite){
        LoginUser loginUser = getLoginUser();
        if(loginUser.getArtUser() == null) return R.fail("用户未登录");
        Long userid = loginUser.getArtUser().getId();
        if(userid == null)return R.fail("用户未找到");
        artFavorite.setUserId(userid);
        return R.ok(artFavoriteService.insertArtFavorite(artFavorite));
    }
    /**
     * 取消收藏
     */
    @DeleteMapping("/f")
    public R<Integer> deleteFavorite(@Validated @RequestBody ArtFavorite artFavorite){
        return R.ok(artFavoriteService.deleteArtFavoriteByFavoriteId(artFavorite.getFavoriteId()));
    }

    /**
     * 导出艺术品列表
     */
    @PreAuthorize("@ss.hasPermi('system:rental:export')")
    @Log(title = "艺术品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArtRental artRental)
    {
        List<ArtRental> list = artRentalService.selectArtRentalList(artRental);
        ExcelUtil<ArtRental> util = new ExcelUtil<ArtRental>(ArtRental.class);
        util.exportExcel(response, list, "艺术品数据");
    }


    @GetMapping(value = { "/art" })
    public AjaxResult art()
    {
        AjaxResult ajax = AjaxResult.success();
//            ajax.put("productCategory",);
//            ajax.put("productStyles", );
//            ajax.put("productPlacement", );
//            ajax.put("productTheme", );
        return ajax;
    }


    /**
     * 获取艺术品详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        ArtRental artRental = artRentalService.selectArtRentalById(id);
        List<PCategory> pCategories = iPCategoryService.selectPCategoryList(new PCategory(null,id,"0"));
        List<PPlacement> pPlacements = iPPlacementService.selectPPlacementList(new PPlacement(null, id, "0"));
        List<PStyle> pStyles = iPStyleService.selectPStyleList(new PStyle(null, id, "0"));
        List<PTheme> pThemes = iPThemeService.selectPThemeList(new PTheme(null, id, "0"));
        artRental.setpIds(pPlacements.stream().map(PPlacement::getValId).toArray(Long[]::new));
        artRental.setcIds(pCategories.stream().map(PCategory::getValId).toArray(Long[]::new));
        artRental.settIds(pThemes.stream().map(PTheme::getValId).toArray(Long[]::new));
        artRental.setsIds(pStyles.stream().map(PStyle::getValId).toArray(Long[]::new));
        return success(artRental);
    }

    /**
     * 新增艺术品
     */
    @PreAuthorize("@ss.hasPermi('system:rental:add')")
    @Log(title = "艺术品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ArtRental artRental)
    {
        return toAjax(artRentalService.insertArtRental(artRental));
    }

    /**
     * 修改艺术品
     */
    @PreAuthorize("@ss.hasPermi('system:rental:edit')")
    @Log(title = "艺术品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArtRental artRental)
    {
       // ArtRental artRental1 = artRentalService.selectArtRentalById(artRental.getId());
       // if(!"0".equals(artRental1.getRentalStatus()))return AjaxResult.error("该商品状态，不能修改！");
        return toAjax(artRentalService.updateArtRental(artRental));
    }

    /**
     * 删除艺术品
     */
    @PreAuthorize("@ss.hasPermi('system:rental:remove')")
    @Log(title = "艺术品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(artRentalService.deleteArtRentalByIds(ids));
    }


    /**
     * 设置商品折扣
     */
    @PostMapping("/setdiscount")
    public AjaxResult setshopdiscount(@RequestBody ArtRental artRental){
        artRentalService.setshopdiscount(artRental);
        return AjaxResult.success();
    }
    /**
     * 设置商品折扣
     */
    @PostMapping("/setspecial")
    public AjaxResult setspecial(@RequestBody ArtRental artRental){
        artRentalService.setspecial(artRental);
        return AjaxResult.success();
    }
}
